package io.renren.modules.module.controller;

import io.renren.common.utils.R;
import io.renren.modules.module.entity.Module;
import io.renren.modules.module.entity.vo.AllModule;
import io.renren.modules.module.entity.vo.AllProductUser;
import io.renren.modules.module.service.ModuleServicex;
import io.renren.modules.product.entity.ProductEntity;
import io.renren.modules.product.entity.ProductUserEntity;

import io.renren.modules.product.service.ProductService;

import io.renren.modules.product.service.ProductUserService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author miaotongli
 */
@RestController
@RequestMapping("/module")
@Api(tags = "模块管理")
public class ModuleControllerx {
    @Autowired
    private ModuleServicex moduleService;
    @Autowired
    private ProductService productService;

    /**
     * 新增模块信息
     * @param module 前端传过来的信息
     * @param result 经过JSR303校验后的结果
     * @return R成功或失败
     */
    @PostMapping("/insert")
    @ApiOperation("新增模块测试")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("module:insert")
    @Transactional
    public R insertModule(@Validated @RequestBody Module module, BindingResult result){
        if (result.hasErrors()){
            HashMap<String, String> errorMap = new HashMap<>();
            result.getFieldErrors().forEach((item)->{
                //获得到错误提示
                String message = item.getDefaultMessage();
                //获取错误的属性名字
                String field = item.getField();
                errorMap.put(field,message);
            });
            return R.error(400,"提交的数据不合法").put("data",errorMap);
        }else {
            Boolean saveInfo = moduleService.saveInfo(module);
            if (saveInfo){
                return R.ok("200").put("data","新增成功");
            }
        }
        return R.error(400,"模块已存在请重新编写");
    }



    @GetMapping("/removeModule/{moduleId}")
    @ApiOperation("删除指定的模块")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @Transactional
    @RequiresPermissions("module:removeModule")
    public R deleteModule(@PathVariable("moduleId") Long moduleId){
         moduleService.deleteModule(moduleId);
         return R.ok("200").put("data","删除成功");
    }

    /**
     * 显示指定产品下的模块分页信息
     * @param productId
     * @return
     */
    @PostMapping("/AllModules/{productId}/{page}/{limit}")
    @ApiOperation("获取指定产品下的模块分页列表")
    @RequiresPermissions("module:AllModules")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R getAllModules(@ApiParam(name = "productId",value = "产品id",required = true) @PathVariable Long productId,
                           @ApiParam(name = "page",value = "当前页面",required = true) @PathVariable Long page,
                           @ApiParam(name = "limit",value = "每页记录数",required = true) @PathVariable Long limit,
                           @RequestBody Map<String,String> map){
        List<AllModule> module = moduleService.getModule(productId, page, limit,map.get("moduleName"));
        return R.ok("200").put("data",module);
    }

    @PostMapping("/updateModule")
    @ApiOperation("更新指定的模块")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("module:updateModule")
    public R updateModule(@RequestBody Module module){
        //模块跟新
        boolean flag = moduleService.updateModule(module);
        if (flag){
            return R.ok("200").put("data","更新成功");
        }
        return R.error(404,"此产品下已存在该模块请重新填写");
    }

    @GetMapping("/selectFdMe/{moduleId}")
    @ApiOperation("回显指定的模块的数据")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("module:selectFdMe")
    public R selectFindMoudle(@PathVariable Long moduleId){
        AllModule allModule=moduleService.getModuleMeaasge(moduleId);
        return R.ok("200").put("data",allModule);
    }


    /**
     * 根据产品id获取该产品下所有的模块
     * @return
     */
    @GetMapping("/getAllModuleByProductId")
    @ApiOperation("根据产品id获取该产品下所有的模块")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("module:getAllModuleByProductId")
    public R getAllModuleByProductId(Long productId){
        if (productId == null) R.error("产品id不能为空");
        List<Module> list = moduleService.getAllModuleByProductId(productId);
        return R.ok().put("data",list);
    }

}
